<?php

namespace App\Controller\Admin;

use App\Controller\Example\ConfigExample;
use App\Models\AdminModel;
use App\Models\ConfigModel;
use App\Models\Conn;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;

class Config extends ConfigModel
{
    /**
     * 管理后台用户配置列表
     * entry token,uid,name,mobile,startTime,endTime,page,limit
     * return json
     * */
    public function configList($data){
        $identical = Common::identical($data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $identical->token);
        if(empty($adminInToken) || $adminInToken["level"] !=1){
            //Result::error(ErrorEnum::NOPERMISSIONTOSUBMIT);
        }
        $joinSql = " ";
        $sql = "SELECT `id`, `name`, `val`, `company`, `status`, `time`, `remarks`,`type` FROM `config` $joinSql order by id asc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($sql,true);
        foreach ($readConn as $item=>$value){
            switch ($value["status"]){
                case 1:$readConn[$item]["status"]= "开启";break;
                default:$readConn[$item]["status"]= "关闭";break;
            }
            switch ($value["type"]){
                case 1:$readConn[$item]["type"]= "奖励类型";break;
                default:$readConn[$item]["type"]= "非奖励类型";break;
            }
        }
        $returnData["list"]=$readConn;
        $countSql="SELECT count(id) as count FROM `config` $joinSql";
        $returnData["count"] = $conn->readConn($countSql)["count"];
        $returnData["total"] = null;
        Result::success($returnData);
    }
    /**
     * 管理后台配置修改
     * entry token,id,name,val,company,status,remarks
     * return json
     * */
    public function setConfigVal($data){
        $assemble = Common::assemble(new ConfigExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        if(empty($adminInToken) || $adminInToken["level"] !=1){
            Result::error(ErrorEnum::NOPERMISSIONTOSUBMIT);
        }
        $conn->shiwuStart();
        if(self::upConfigInside($conn,$assemble) != 1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::MODIFICATIONFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"全局配置","修改配置:[".$assemble->name."]:".$assemble->val);
        Result::success("ok");
    }
}